﻿using System;
using System.Collections.Generic;
using System.Threading;
using Cysharp.Threading.Tasks;
using DG.Tweening;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Rendering;
using WssUtils;

namespace JQMergeFramework
{
    public class CityUnit: UnitBase
    {
        //是否是初始放置的三个房子
        public bool isInitTowCity = false;

        public void SetInitTowCity()
        {
            isInitTowCity = true;
        }

        public void Init(Mesh ms,UnitDataBase data)
        {
            mUnitData = data;
            
            mMeshFilter.mesh = ms;
            //更新宽高
            mMeshSize = mMeshRenderer.bounds.size;
            mMeshRenderer.shadowCastingMode  = ShadowCastingMode.On;
            mMeshRenderer.transform.localRotation = Quaternion.Euler(new Vector3(0,125,0));
            
            base.RefreshLevel();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="bTrack"></param>
        public void BindCityTrack(TrackUnit bTrack)
        {
            mTrackUnit = bTrack;
            mTrackUnit.BindCityUnit(this);
            this.transform.localScale = Vector3.one;
        }


        //增加合成分数
        public void AddMergeScore()
        {
            int addScore = (Level * 3);
            DataManager.Instance.CurrentScore += addScore;
            //增加分数
            UICityLvTips.I.ShowFillScoreTips(addScore);
        }

        /// <summary>
        /// 绑定地形格子
        /// </summary>
        /// <param name="_groundBase"></param>
        public void BindGround(GroundBase _groundBase,bool isMovePath = false)
        {
            mGroundBase = _groundBase;
            
            Vector3 groundPos = _groundBase.transform.position;
            
            this.transform.DOKill();
            this.transform.SetParent(CityBaseControl.I.cityPlaceParent);
            this.transform.localScale = Vector3.one;
            
            
            Sequence sq = DOTween.Sequence();
            if (isMovePath)
            {
                sq.Append(this.transform.DOMove(groundPos + new Vector3(0, 10, 0), 0.2F));
            }
            else
            {
                this.transform.position = groundPos + new Vector3(0,10,0);
            }
            
            //增加放置分数
            if (!isMovePath && !isInitTowCity)
            {
                DataManager.Instance.CurrentScore += (Level+1);
                UICityLvTips.I.ShowLvTips(this);
            }
            
            
            sq.Append(this.transform.DOMoveY(groundPos.y, GameCfg.DownTime).SetEase(Ease.OutBack));
            sq.AppendCallback(() =>
            {
                audioCtr.Play(GetCityPlaceSound());
            });
            sq.Play();
            
            mDefaultPos = groundPos;
            
            //放置特效
            GameObject  effectObj = EffectPool.I.GetPoolObject(PoolName.SmokeCircle);
            effectObj.transform.position = mGroundBase.transform.position;
            effectObj.GetComponent<ParticleSystem>().Play();
            
            EffectPool.I.PutPoolObject(PoolName.SmokeCircle,effectObj,1);
            
            // SetShowCollider(true);
            DefaultMeshMat();
        }
        

        public void OnCreateAnim(Transform targetTrans)
        {
            this.mTransform.DOKill();
            this.mTransform.position = targetTrans.position + (Vector3.up * 2);
            this.mTransform.DOMoveY(targetTrans.position.y,GameCfg.DownTime).SetEase(Ease.Linear);
        }
        
        
        public void RDestroy()
        {
            this.mGroundBase?.ClareCity();
            this.mGroundBase = null;
            this.mTransform.DOKill();
            mMeshRenderer.transform.DOKill();
            DOTween.Kill(this.mTransform.GetInstanceID());
            
            mMeshFilter.mesh = null;
        
            DefaultMeshMat();
            CityPool.I.Put(this);
        }

        
        //删除绑定的履带，不带动画，直接删除
        public void DestroyTrack()
        {
            mTrackUnit?.PutTrack();
            mTrackUnit = null;
        }

        //删除绑定的履带,带移动动画
        public void PutTrack()
        {
            mTrackUnit?.PutTrackAnim();
            mTrackUnit = null;
        }
        
        string GetCityPlaceSound()
        {
            int rd = MyRandom.Range(0, 3);
            switch (rd)
            {
                case 0:
                    return AudioName.CityPlace1;
                    break;
                case 1:
                    return AudioName.CityPlace2;
                    break;
                case 2:
                    return AudioName.CityPlace3;
                    break;
            }
            return AudioName.CityPlace1;
        }

        //更换材质
        public void ChangeMeshMat()
        {
            //更改材质
            mMeshRenderer.material = MergeManager.Instance.scriptablePrefab.removeRedMat;
        }

        //取消除操作
        public void DefaultMeshMat()
        {
            //还原材质
            mMeshRenderer.material = MergeManager.Instance.scriptablePrefab.litCityDefMat;
        }

        //删除道具表现
        public void PlayAnimRemoveProp()
        {
           GameObject mHammerModel =  EffectPool.I.GetPoolObject(PoolName.Hammer);
            
           mHammerModel.transform.SetParent(this.mTransform);
     
           
            mHammerModel.GetComponent<HammerAnim>().PlayAnim(() =>
            {
                mHammerModel.transform.SetParent(null);
                RDestroy();
            });
        }
        
        
        //移动到指定位置
        public override void Move(Vector3 targetPos,float t = 0.2f)
        {
            this.mTransform.DOKill();
            this.mTransform.DOMove(targetPos, t);
            base.Move(targetPos);
        }
        
        //移动到默认放置的位置
        public override void MoveDefaultPlacePos()
        {
            this.mTransform.DOKill();
            this.mTransform.DOMove(mDefaultPos, 0.2F);
            base.MoveDefaultPlacePos();
        }
        
        //设置为半透明状态
        public override void SetTransparencyMat()
        {
            Material transparencyMat = MergeManager.Instance.scriptablePrefab.transparencyMat;
            if (mMeshRenderer.material != transparencyMat)
            {      
                mMeshRenderer.material = transparencyMat;
            }
            base.SetTransparencyMat();
        }

        //设置为默认状态
        public override void SetDefaultMat()
        {
            Material litCityDefMat = MergeManager.Instance.scriptablePrefab.litCityDefMat;
            if (mMeshRenderer.material != litCityDefMat)
            {
                mMeshRenderer.material = litCityDefMat;
            }
            base.SetDefaultMat();
        }
        

        
    }
}